﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Cloudthentic.SSIS.Server
{
    /// <summary>
    /// Ssis Application Server
    /// </summary>
    public class SsisApplication
    {
        static Microsoft.SqlServer.Dts.Runtime.Application application;

        public SsisApplication() { }

        public static Microsoft.SqlServer.Dts.Runtime.Application Application
        {
            get
            {
                if (application == null) application = new Microsoft.SqlServer.Dts.Runtime.Application();
                return application;
            }
        }

        public void ExecutePackage(
            string packageName,
            System.Collections.Generic.List<SsisVariable> variables)
        {

            Executor executor = new Executor();
            executor.SsisServerConfiguration = this.GetReadOnlyConfiguration();
            executor.Execute(packageName, variables);
        }


        public ConfigurationSection GetConfiguration()
        {
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
            ConfigurationSection section = config.Sections.Get(typeof(Configuration.SsisServerConfiguration).Name);
            return section;

        }

        public Configuration.SsisServerConfiguration GetReadOnlyConfiguration()
        {
            Configuration.SsisServerConfiguration configuration = System.Configuration.ConfigurationManager.GetSection(typeof(Configuration.SsisServerConfiguration).Name) as Configuration.SsisServerConfiguration;
            if (configuration == null) throw new NullReferenceException("SsisApplication::GetConfiguration");
            
            return configuration;
        }

    }
}
